package io.coderyeah.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.coderyeah.basic.Result;
import io.coderyeah.basic.service.BaiduAiAuditService;
import io.coderyeah.basic.util.LoginInfoHolder;
import io.coderyeah.org.domain.Employee;
import io.coderyeah.org.mapper.EmployeeMapper;
import io.coderyeah.product.domain.Product;
import io.coderyeah.product.domain.ProductDetail;
import io.coderyeah.product.domain.ProductOnlineAuditLog;
import io.coderyeah.product.dto.ProDTO;
import io.coderyeah.product.mapper.ProductDetailMapper;
import io.coderyeah.product.mapper.ProductMapper;
import io.coderyeah.product.mapper.ProductOnlineAuditLogMapper;
import io.coderyeah.product.service.ProductService;
import io.coderyeah.user.domain.LoginInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author coderyeah
 * @description 针对表【t_product】的数据库操作Service实现
 * @createDate 2022-09-27 09:51:42
 */
@Service
@Transactional
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {
    @Resource
    private ProductMapper productMapper;
    @Resource
    private ProductDetailMapper detailMapper;
    @Resource
    private ProductOnlineAuditLogMapper auditLogMapper;
    @Resource
    private BaiduAiAuditService baiduAiAuditService;
    @Resource
    private EmployeeMapper employeeMapper;

    @Override
    public Result saveOrModify(Product product) {
        if (null != product.getId()) {
            // 修改操作
            productMapper.updateById(product);
            // 修改详情
            if (product.getProductDetail() != null) {
                product.getProductDetail().setProductId(product.getId());
                detailMapper.update(product.getProductDetail(), new LambdaUpdateWrapper<ProductDetail>()
                        .eq(ProductDetail::getProductId, product.getId()));
            }
        } else {
            // 添加操作
            productMapper.insert(product);
            // 添加服务详情
            final ProductDetail productDetail = product.getProductDetail();
            if (null != productDetail) {
                productDetail.setProductId(product.getId());
                detailMapper.insert(productDetail);
            }
        }
        return Result.success(null);
    }

    @Override
    public List<Product> getAllPros(ProDTO proDTO) {
        return productMapper.getAllPros(proDTO);
    }

    @Override
    public Result onsale(List<Long> ids) {
        // 上架
        for (int i = 0; i < ids.size(); i++) {
            Product product = productMapper.selectById(ids.get(i));
            // 1.对上架的记录进行文本审核
            Boolean bool1 = baiduAiAuditService.textAudit(product.getName());
            Boolean bool2 = baiduAiAuditService.imageAudit(product.getResources());
            final ProductOnlineAuditLog log = new ProductOnlineAuditLog();
            log.setProductId(product.getId());
            // 添加当前操作人
            final LoginInfo loginInfo = LoginInfoHolder.get();
            final Employee employee = employeeMapper.selectOne(new LambdaQueryWrapper<Employee>().eq(Employee::getLogininfoId, loginInfo.getId()));
            log.setAuditId(employee.getId());
            if (bool1 && bool2) {
                // 审核成功修改状态
                productMapper.onsale(ids.get(i), new Date());
                // 添加审核日志
                log.setState(1);
                log.setAuditTime(new Date());
                log.setNote("审核成功");
                auditLogMapper.insert(log);
            } else {
                // 审核失败
                log.setState(0);
                log.setAuditTime(new Date());
                log.setNote("审核失败");
                auditLogMapper.insert(log);
                return Result.fail("审核失败");
            }
        }
        return Result.success("审核成功");
    }

    @Override
    public Result offsale(List<Long> ids) {
        // 批量下架
        productMapper.offsale(ids, new Date());
        return Result.success(null);
    }

    @Override
    public Result getPetById(Long id) {
        Product p = productMapper.getPetById(id);
        return Result.success(p);
    }
}




